package com.zt.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zt.feign.UserFeign;
import com.zt.system.FinancialProduct;
import com.zt.service.FinancialProductService;
import com.zt.mapper.FinancialProductMapper;
import com.zt.utils.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author 不.凌
* @description 针对表【financial_product】的数据库操作Service实现
* @createDate 2024-09-03 17:43:57
*/
@Service
public class FinancialProductServiceImpl extends ServiceImpl<FinancialProductMapper, FinancialProduct>
    implements FinancialProductService{
    @Resource
    private FinancialProductMapper financialProductMapper;
    private Integer a;


    @Override
    public Result financialProductList() {
        List<FinancialProduct> financialProducts = financialProductMapper.selectList(null);
        return Result.success(financialProducts);
    }

    @Override
    public Result financialProductById(Integer productId) {
        FinancialProduct financialProduct = financialProductMapper.selectById(productId);
        return Result.success(financialProduct);
    }

    @Override
    public FinancialProduct getFinancialProductById(Integer transactionId) {
        FinancialProduct financialProduct = financialProductMapper.selectById(transactionId);
        return financialProduct;
    }

    @Override
    public Result financialProductByNumber(Integer number,String productName) {
        Double redemptionRate = financialProductMapper.getRedemptionRate(number,productName);
        return Result.success(redemptionRate);
    }

    @Override
    public Result getBoughtProduct(Integer number, String productName) {
        Double getBoughtProduct = financialProductMapper.getBoughtProduct(number, productName);
        return Result.success(getBoughtProduct);
    }

    @Override
    public Result updateNumber(Integer productId,Integer number) {
        FinancialProduct financialProduct = financialProductMapper.selectById(productId);
        financialProduct.setNumber(financialProduct.getNumber()-number);
        if(financialProduct.getNumber()<0){
            return Result.error("购买失败，库存不足");
        }
        financialProductMapper.updateById(financialProduct);
        return Result.success("购买成功");
    }

    @Override
    public Result addNumber(Integer productId, Integer number) {
        FinancialProduct financialProduct = financialProductMapper.selectById(productId);
        financialProduct.setNumber(financialProduct.getNumber()+number);
        financialProductMapper.updateById(financialProduct);
        return Result.success("购买成功");
    }
}




